/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helwen.restaurantbooking.bo.boimpl;

import com.helwen.restaurantbooking.bo.ITableBo;
import com.helwen.restaurantbooking.datalayer.dao.IBookingDao;
import com.helwen.restaurantbooking.datalayer.dao.ITableDao;
import com.helwen.restaurantbooking.datalayer.entities.TblBooking;
import com.helwen.restaurantbooking.datalayer.entities.TblTable;
import com.helwen.restaurantbooking.helper.RBResourceBundle;
import com.helwen.restaurantbooking.ui.customcomponent.TableComponent;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.ImageIcon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Nghiata
 */
@Service("tableBo")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true, isolation = Isolation.DEFAULT)
public class TableBoImpl implements ITableBo {

    @Autowired
    private ITableDao tableDao;
    @Autowired
    private IBookingDao bookingDao;

    public List<TableComponent> getAllTable() {
        List<TableComponent> tableComponents = new ArrayList<TableComponent>();
        List<TblTable> tblTables = tableDao.getAllTable();
        for (TblTable tblTable : tblTables) {
            TableComponent tbl = new TableComponent();
            tbl.setTableNumber(tblTable.getTableNumber());
            tbl.setArea(tblTable.getArea());
            tbl.setStatus(tblTable.getStatus());
            if (tblTable.getStatus() == 0) {
                tbl.setIcon(new ImageIcon(this.getClass().getResource(RBResourceBundle.getValue("icon_release_table"))));
            } else if (tblTable.getStatus() == 1) {
                tbl.setIcon(new ImageIcon(this.getClass().getResource(RBResourceBundle.getValue("icon_reserved_table"))));
            } else {

                tbl.setIcon(new ImageIcon(this.getClass().getResource(RBResourceBundle.getValue("icon_occupied_table"))));
            }

            tableComponents.add(tbl);
        }
        return tableComponents;
    }

    public ITableDao getTableDao() {
        return tableDao;
    }

    public void setTableDao(ITableDao tableDao) {
        this.tableDao = tableDao;
    }

    public IBookingDao getBookingDao() {
        return bookingDao;
    }

    public void setBookingDao(IBookingDao bookingDao) {
        this.bookingDao = bookingDao;
    }

    public TblBooking getYellowRedTableBookingInfo(Integer tableNumber) {
        Set<TblBooking> tblBookings = new HashSet<TblBooking>();
        tblBookings = tableDao.getTableByNumber(tableNumber).getTblBookings();
        TblBooking object = null;
        Date date = new Date();
        for (TblBooking tblBooking : tblBookings) {
            Date minimumDateTime = new Date(tblBooking.getReservationDate().getTime() + tblBooking.getReservationTime() * 3600 * 1000 - 1800 * 1000);
            Date maximumDateTime = new Date(tblBooking.getReservationDate().getTime() + tblBooking.getReservationTime() * 3600 * 1000 + 1800 * 1000);

            if (date.getTime() > minimumDateTime.getTime() && date.getTime() < maximumDateTime.getTime() && tblBooking.getStatus() == 0) {
                object = tblBooking;
            }
            if (tblBooking.getTblTable().getStatus() == 2 && tblBooking.getStatus() == 1) {
                object = tblBooking;
            }
        }
        return object;
    }

    public TblTable getTableByNumber(Integer tableNumber) {
        return tableDao.getTableByNumber(tableNumber);
    }

    @Transactional(readOnly = false)
    public boolean updateListTable() {
        List<TblBooking> tblBookings = bookingDao.getWaitingBooking();
        Date date = new Date();
        System.out.println(date);
        boolean isHasChange = false;
        for (TblBooking tblBooking : tblBookings) {
            Date minimumDateTime = new Date(tblBooking.getReservationDate().getTime() + tblBooking.getReservationTime() * 3600 * 1000 - 1800 * 1000);

            Date maximumDateTime = new Date(tblBooking.getReservationDate().getTime() + tblBooking.getReservationTime() * 3600 * 1000 + 1800 * 1000);


            if (date.getTime() > minimumDateTime.getTime() && date.getTime() < maximumDateTime.getTime()) {

                TblTable tblTable = tblBooking.getTblTable();
                tblTable.setStatus(new Short("1"));
                tableDao.saveOrUpdate(tblTable);
                isHasChange = true;
            } else if (date.getTime() >= maximumDateTime.getTime()) {
                tblBooking.setStatus(new Short("3"));
                tblBooking.getTblTable().setStatus(new Short("0"));
                bookingDao.saveOrUpdate(tblBooking);
                isHasChange = true;
            }
        }
        return isHasChange;
    }
}
